package com.hydra.wiki.doc.controller;

import com.hydra.wiki.core.page.ViewMode;
import com.hydra.wiki.core.sql.query.DBSort;
import com.hydra.wiki.core.sql.query.DataQuery;
import com.hydra.wiki.core.sql.result.DataResult;
import com.hydra.wiki.doc.server.FarmDocManagerInter;
import com.hydra.wiki.doc.server.FarmDocRunInfoInter;
import com.hydra.wiki.web.WebUtils;
import com.hydra.wiki.web.easyui.EasyUiUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/* *
 *功能：文档发布排名控制层
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20150707114057
 *说明：
 */
@RequestMapping("/farmReleaseRanking")
@Controller
public class FarmReleaseRankingController extends WebUtils {
    private final static Logger log = Logger.getLogger(FarmReleaseRankingController.class);

    @Resource
    private FarmDocManagerInter farmDocManagerImpl;
    @Resource
    private FarmDocRunInfoInter farmDocRunInfoImpl;

    /**
     * 查询结果集合
     *
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> queryall(DataQuery query, HttpServletRequest request) {
        try {
            query = EasyUiUtils.formatGridQuery(request, query);
            query.addSort(new DBSort("B.GOODRATE", "DESC"));
            DataResult result = farmDocRunInfoImpl.createReleaseRankingSimpleQuery(query).search();
            return ViewMode.getInstance()
                    .putAttrs(EasyUiUtils.formatGridData(result))
                    .returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setError(e.getMessage())
                    .returnObjMode();
        }
    }

    @RequestMapping("/list")
    public ModelAndView index(HttpSession session) {
        return ViewMode.getInstance().returnModelAndView("doc/FarmReleaseRankingResult");
    }
}
